@tool
class_name AdditionalEffect_Action_PlayAnimation
extends AdditionalEffect_Action

#region 属性
@export var anim_name:String
@export var is_register_anim_finished_event:bool = true
@export var is_auto_stop_by_exited:bool = true
@export var anim_speed_scale_attribute:String
## 目标固定播放时间
@export var target_play_time_cb:CompositeProperty
@export var anim_finished_additional_effects:Array[AdditionalEffect]
var anim_finished_callback:Callable
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(args:Dictionary = {}):
	if super(args):
		for additional_effect in anim_finished_additional_effects:
			self.args["Target"] = target
			self.args["Owner"] = self
			self.args["Action"] = action
			additional_effect.init(args)
		return true
	return false
## 检查能否执行添加效果，默认为true
func can_excute() -> bool:
	return super()
## 执行添加效果
func excute() -> bool:
	if super():
		target = target as Entity
		var animation_component:AnimationComponent = target.get_component(AnimationComponent)
		if animation_component:
			var anim_speed = 1
			var animation:Animation = animation_component.get_animation(anim_name)
			var play_time:float = animation.length
			if anim_speed_scale_attribute:
				anim_speed = target.entity_data.get_attribute_value(anim_speed_scale_attribute,1)
			if target_play_time_cb != null and target_play_time_cb.get_value(target) != null and target_play_time_cb.get_value(target) > 0:
				anim_speed = (play_time * anim_speed) / target_play_time_cb.get_value(target)
			animation_component.play_animation(anim_name,-1,anim_speed)
			if is_register_anim_finished_event:
				anim_finished_callback = func ():
					action.manager.chart.set_expression_property("AnimFinished_" + anim_name,true)
					for additional_effect in anim_finished_additional_effects:
						additional_effect.excute()
				animation_component.register_anim_finished_event(anim_name,anim_finished_callback,
					Object.CONNECT_ONE_SHOT
				)
				action.register_state_event(StateChartStateActionBase.ActionTiming.Exited,on_action_exited,
					Object.CONNECT_ONE_SHOT
				)
			elif is_auto_stop_by_exited:
				action.register_state_event(StateChartStateActionBase.ActionTiming.Exited,on_action_exited,
					Object.CONNECT_ONE_SHOT
				)
		return true
	else:
		return false
## 取消执行的效果
func cancel_excute() -> bool:
	if super():
		target = target as Entity
		var animation_component:AnimationComponent = target.get_component(AnimationComponent)
		if animation_component:
			if animation_component.animation_player.current_animation == anim_name:
				animation_component.animation_player.stop()
				animation_component.unregister_anim_finished_event(anim_name,anim_finished_callback)
				action.unregister_state_event(StateChartStateActionBase.ActionTiming.Exited,on_action_exited)
		return true
	else:
		return false
func on_action_exited():
	target = target as Entity
	var animation_component:AnimationComponent = target.get_component(AnimationComponent)
	if animation_component:
		if animation_component.animation_player.current_animation == anim_name and is_auto_stop_by_exited:
			animation_component.animation_player.stop()
		if is_register_anim_finished_event:
			action.manager.chart.set_expression_property("AnimFinished_" + anim_name,null)
			animation_component.unregister_anim_finished_event(anim_name,anim_finished_callback)
		action.unregister_state_event(StateChartStateActionBase.ActionTiming.Exited,on_action_exited)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
#endregion
